import math
import numpy as np

points = [
    # start
    (34.1, -215),
    # (34.1, -113),
    # (160, -260),
    # 矩形
    (46.1, -185),
    (192.1, -185),
    (192.1, -238),
    (46.1, -238),
    # 五边形
    (81.1, -32),
    (114.1, -87),
    (88.1, -159),
    (39.1, -145),
    (31.1, -89),
    # 三角形
    (133.1, -77),
    (154.1, -163),
    (114.1, -163),
    # 歪着的四边形
    (155.1, -37),
    (190.1, -32),
    (214.1, -64),
    (155.1, -107),
    # 三角形
    (189.1, -134),
    (232.1, -178),
    (202.1, -214),
    # 竖着的矩形
    (220.1, -38),
    (251.1, -38),
    (251.1, -152),
    (220.1, -152),
    # 六边形
    (243.1, -182),
    (274.1, -155),
    (298.1, -181),
    (298.1, -225),
    (269.1, -243),
    (243.1, -223),
    # 四边形
    (286.1, -36),
    (303.1, -57),
    (294.1, -165),
    (264.1, -54),

    (331.1, -42)
]

def calculate_distance(point1, point2):
    # 将输入的坐标转换为元组
    x1, y1 = point1
    x2, y2 = point2

    # 计算两点间的距离
    distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
    distance = round(distance, 2)   # 保留两位小数

    return distance


def inline(point1, point2, point3):
    x1, y1 = point1
    x2, y2 = point2
    x3, y3 = point3
    return (y3 - y1) * (x2 - x1) == (y2 - y1) * (x3 - x1)

def ccw(point1, point2, point3):
    x1, y1 = point1
    x2, y2 = point2
    x3, y3 = point3
    # 点与向量的叉积
    return (y3 - y1) * (x2 - x1) > (y2 - y1) * (x3 - x1)

def intersect(point1, point2, point3, point4):
    if inline(point1, point3, point4) or inline(point2,point3,point4) or \
        inline(point3, point1, point2) or inline(point4, point1, point2):
        return False
    return ccw(point1, point3, point4) != ccw(point2,point3,point4) and\
        ccw(point1, point2, point3) != ccw(point1, point2, point4)


def line_polygon_intersection(segment_start, segment_end, polygon):

    # 判断线段是否全部在多边形内
    if (segment_start in polygon and segment_end in polygon):
        for i in range(len(polygon)):
            if (segment_start == polygon[i] and segment_end == polygon[(i + 1) % len(polygon)]) or \
                    (segment_start == polygon[(i + 1) % len(polygon)] and segment_end == polygon[i]):
                return False
        return True

    # 检查线段是否和多边形的边相交
    for i in range(len(polygon)):
        if intersect(segment_start, segment_end, polygon[i], polygon[(i + 1) % len(polygon)]):
            return True
    return False

def data():
    """
    从points点集中构建多边形
    :return: 多边形集合
    """
    polygons = []
    polygons_points = []
    # print(len(points))
    start = 0; end = 0
    ge = [4, 9, 12, 16, 19, 23, 29, 33]
    for i in range(1, len(points)):
        polygons_points.append(points[i])
        if i in ge:
            end = i
            polygons.append(polygons_points[start:end])
            start = end
    print(len(polygons))
    print(intersect(points[1], points[13], points[5], points[9]))
    return polygons

def getMartix():
# if __name__ == '__main__':
    polygons = data()
    # print(polygons)
    # 构建邻接矩阵
    martix = np.zeros((35, 35))
    for i in range(35):
        for j in range(35):
            if (i == j): continue
            is_ok = True
            for item in polygons:
                # 判断线段一个线段是否和一个多边形相交
                if line_polygon_intersection(points[i], points[j], polygon=item):
                    is_ok = False; break
            if (is_ok):
                # 计算两点的距离
                martix[i][j] = calculate_distance(points[i], points[j])
                martix[j][i] = martix[i][j]
            else:
                martix[i][j] = 0
                martix[j][i] = 0
    # 矩形
    for i in range(1, 5):
        j = i + 1
        if (j > 4):
            j = (i + 1) % 4
        # print('i: {} , j: {}'.format(i,j))
        martix[i][j] = calculate_distance(points[i], points[j])
    for i in range(5, 10):
        j = i + 1
        if (j > 9):
            j = (i + 1) % 9
        martix[i][j] = calculate_distance(points[i], points[j])
    for i in range(10, 13):
        j = i + 1
        if (j > 12):
            j = j % 12
        martix[i][j] = calculate_distance(points[i], points[j])
    for i in range(13, 17):
        j = i + 1
        if (j > 16):
            j = j % 16
        martix[i][j] = calculate_distance(points[i], points[j])
    for i in range(17, 20):
        j = i + 1
        if j > 19:
            j = j % 19
        martix[i][j] = calculate_distance(points[i], points[j])
    for i in range(20, 24):
        j = i + 1
        if j > 23:
            j = j % 23
        martix[i][j] = calculate_distance(points[i], points[j])
    for i in range(24, 30):
        j = i + 1
        if j > 29:
            j = j % 29
        martix[i][j] = calculate_distance(points[i], points[j])
    for i in range(30, 34):
        j = i + 1
        if j > 33:
            j = j % 33
        martix[i][j] = calculate_distance(points[i], points[j])
    # for i in range(35):
    #     for j in range(35):
    #         print(martix[i][j], end="  ")
        # print()
    print(martix[1][13])
    return martix